import router from '@system.router';
import dataStorage from '@ohos.data.storage'
import ClassApi from '../../api/classApi.js';
import CourseApi from '../../api/courseApi.js';
import prompt from '@system.prompt';

export default {
    data: {
        baseImageUrl: '',
        studentId: 0,
        tab: 0,
        fresh: false,
        classes: [],
        courses: [],
        total: 0,
        courses_NoNull: false,
        classes_NoNull: false,
    },
    onInit() {
        this.baseImageUrl = this.$app.$def.globalData.baseImageUrl;
        //        let storage = storage.getStorageSync(this.$app.$def.globalData.path);
        //        this.studentId = storage.getSync('user', 0);
        let storage = dataStorage.getStorageSync(this.$app.$def.globalData.path);
        let user = storage.getSync('user', '{“id”:0}');
        this.studentId = JSON.parse(user).id;
    },
    goBack() {
        router.back();
    },
    changeTabs(index) {
        this.tab = index;
        if (index == 0) {
            if (this.studentId == 0) {

            } else {
                CourseApi.getCourseByUserId(this.studentId, (data) => {
                    if (data) {
                        this.courses = data;
                        this.courses_NoNull = true;
                    } else {
                        prompt.showToast({
                            message: '未添加课程'
                        })
                    }
                });
            }
        } else {
            if (this.studentId == 0) {
            }
            else {
                ClassApi.getClassesByStudentId(this.studentId, (data) => {
                    if (data) {
                        this.classes = data;
                        this.classes_NoNull = true;
                    }
                    else {
                        prompt.showToast({
                            message: '未加入班级'
                        })
                    }
                });
            }
        }
    },
    refresh: function (e) {
        let that = this;
        that.fresh = e.refreshing;
        let storage = dataStorage.getStorageSync(this.$app.$def.globalData.path);
        let user = storage.getSync('user', '{“id”:0}');
        this.studentId = JSON.parse(user).id;
        if(this.studentId != 0){
            if(this.tab==0){
                CourseApi.getCourseByUserId(this.studentId, (data) => {
                    if (data) {
                        this.courses = data;
                        this.courses_NoNull = true;
                    } else {
                        prompt.showToast({
                            message: '未添加课程'
                        })
                    }
                });
            }else{
                ClassApi.getClassesByStudentId(this.studentId, (data) => {
                    if (data) {
                        this.classes = data;
                        this.classes_NoNull = true;
                    }
                    else {
                        prompt.showToast({
                            message: '未加入班级'
                        })
                    }
                });
            }
        }else{
            prompt.showToast({
                message: '未登录！！'
            })
        }
        that.fresh = false;
    },

    viewDetail(id) {
        if (this.tab == 0) {
            router.push({
                uri: 'pages/courseDetail/courseDetail',
                params: {
                    id
                }
            });
        } else if (this.tab == 1) {
            router.push({
                uri: 'pages/classDetail/classDetail',
                params: {
                    id
                }
            });
        }

    },
}
